<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程分类控制器
 */
class Subject extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $searchFields = ['subject.id','title', 'cate.name','price'];
    // 初始化
    public function _initialize()
    {
        parent::_initialize();

        // $this->request->filter('trim,strip_tags,htmlspecialchars');

        // 引入的模型
        $this->SubjectModel = model('Subject.Subject');

        // 引入的模型
        $this->CategoryModel = model('Subject.Category');

        $this->rulelist = $this->CategoryModel->select();

        $ruledata = [];

        foreach ($this->rulelist as $k => &$v) {

            $ruledata[$v['id']] = $v['name'];
        }
        $this->view->assign('ruledata', $ruledata);
    }


    // 首页
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
            $this->changeWhereName("id", "subject.id");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SubjectModel
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->SubjectModel
                ->with('category')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 添加
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');

            if ($params) {
                $result = $this->SubjectModel->validate("Common/Subject/Subject")->save($params);

                if ($result === false) {
                    $this->error($this->SubjectModel->getError());
                    exit;
                }

                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->SubjectModel->find($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params['id'] = $ids;
                $result = $this->SubjectModel->validate("Common/Subject/Subject")->isUpdate(true)->save($params);

                if ($result === false) {
                    if($params['thumbs'] && $params['thumbs'] !=  $row['thumbs']) {
                        $thumbs = substr($params['thumbs'], 1); 
                        @is_file($thumbs) && @unlink($thumbs);
                    }
                    $this->error($this->SubjectModel->getError());
                    exit;

                } else {
                    if($params['thumbs'] && $params['thumbs'] !=  $row['thumbs']) {
                        $thumbs = substr($row['thumbs'], 1);
                        @is_file($thumbs) && @unlink($thumbs);
                    }
                }

                $this->success();
                exit;
            }
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->SubjectModel->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('没有找到该课程'));
                exit;
            }
        }

        $result = $this->SubjectModel->destroy($ids);

        if ($result === false) {
            $this->error($this->SubjectModel->getError());
            exit;
        }

        $this->success();
        exit;

    }
    
    public function detail($ids = null)
    {
        $row = $this->SubjectModel->with('category')->find($ids);

        if (!$row) {
            $this->error(__('No Results were found'));
            exit;
        }

        $this->assign("row", $row);

        return $this->view->fetch();
    }
}
